package com.mdp.im.core.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.mdp.core.api.Sequence;
import com.mdp.core.entity.Result;
import com.mdp.core.entity.Tips;
import com.mdp.core.utils.SequenceFormat;
import com.mdp.im.core.publish.api.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController("mdp.im.pushController")
@RequestMapping(value="/**/im/push")
public class PushController {
	
	@Autowired
	PublishService pushService;
	
	@Autowired
	Sequence sequence;

	
	 
	@RequestMapping(value="/publishMessage",method=RequestMethod.POST)
	public Result publishMessage(@RequestBody JSONObject message) {
		Tips tips=new Tips("发送成功");
  		String msgId=(String) message.get("msgId");
		String groupId=(String) message.get("groupId");
		String action=(String) message.get("action");
		String msgType=(String) message.get("msgType");
		String branchId=(String) message.get("branchId");
		String sendUserid=(String) message.get("sendUserid");
		String toUserid=(String) message.get("toUserid");

 		String sendUsername=(String) message.get("sendUsername");
 		Long sendTime =this.getLong(message.get("sendTime")); 
		String store =(String) message.get("store");
		if(StringUtils.isEmpty(branchId)) {
			branchId="platform-branch-001";
		}
		if(StringUtils.isEmpty(sendUserid)) {
			sendUserid="platform-branch-001";
			sendUsername="平台";
		}

		if(StringUtils.isEmpty(msgId)) {
			msgId=SequenceFormat.parse("{date62:yyyyMMddHHmmssS}{rands:4}"); 
		}

		if(sendTime==null) {
			sendTime=new Date().getTime(); 
		}

		if(groupId==null && "group".equals(msgType)) {
			return Result.error("groupId-required","groupId群组编号不能为空");
		}
		if(groupId==null  ) {
			groupId=createPrichatGroupId(sendUserid,toUserid); 
		}
		if(action==null ) {
			return Result.error("action-required","action不能为空");
		}

		if(store==null) {
			store="1";
		}
		if(tips.isOk()) {

			message.put("msgId",msgId ); 
			message.put("groupId", groupId); 
			message.put("action", action); 
			message.put("msgType", msgType); 
			message.put("branchId", branchId); 
			message.put("sendUserid", sendUserid);
	 		message.put("sendUsername", sendUsername); 
	 		message.put("sendTime", sendTime);  
			message.put("store", store); 
			pushService.publishMessage(message);
		}
		return Result.ok();
		
	} 
	public String createPrichatGroupId(String sendUserid,String toUserid) {  
		List<String> userids=new ArrayList<>(); 
    		userids.add(sendUserid);
    		userids.add(toUserid);
    		Collections.sort(userids);
    		String groupId=userids.get(0)+"_"+userids.get(1); 
    		return groupId;
	}
	
	public   Long getLong(Object v){
		if(v==null){
			return null;
		}
		Long amount=(long) 0;
		Object amountObj=v;
		if(amountObj instanceof Long){
			amount=(Long)amountObj;
		}else if(amountObj instanceof Double){
			amount=((Long)amountObj).longValue();
		}else{
			amount=Long.valueOf((String) amountObj);
		}
		return amount;
	}
}
